package com.carrental.api.pms.controller;


import com.carrental.api.cms.domain.CmsCar;
import com.carrental.api.cms.service.ICmsCarService;
import com.carrental.common.response.CommonPage;
import com.carrental.common.response.CommonResult;
import com.carrental.api.common.query.CommonQueryParam;
import com.carrental.api.pms.domain.PmsStore;
import com.carrental.api.pms.domain.bo.PmsStoreBo;
import com.carrental.api.pms.domain.vo.PmsStoreVo;
import com.carrental.api.pms.service.IPmsStoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 梁毅
 * @since 2022-03-07 23:06:16
 */
@RestController
@Api(tags = "门店管理")
@RequestMapping("/pms/store")
public class PmsStoreController {
    @Autowired
    private IPmsStoreService storeService;
    @Autowired
    private ICmsCarService cmsCarService;
    @PostMapping()
    @ApiOperation("创建网点")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> storeCreate(@RequestBody PmsStoreBo storePost) {
        return CommonResult.success(storeService.create(storePost));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除网点信息")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> storeRemove(@ApiParam(name = "id", value = "网点标识", required = true)
                                             @PathVariable Long id) {
        return CommonResult.success(storeService.delete(id));
    }

    @PutMapping("/{id}")
    @ApiOperation("修改网点信息")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> storeUpdate(@ApiParam(name = "id", value = "网点标识", required = true)
                                             @PathVariable Long id, @RequestBody PmsStoreBo storePost) {
        return CommonResult.success(storeService.update(id,storePost));
    }

    @GetMapping()
    @ApiOperation("获取网点列表")
    public CommonResult<List<PmsStore>> storeList(CommonQueryParam commonQueryParam) {
        return CommonResult.success(storeService.list(commonQueryParam));
    }

    @GetMapping("/{storeId}/car")
    @ApiOperation("获取网点下属车辆列表")
    public CommonResult<List<CmsCar>> storeList(@PathVariable Long storeId,@RequestParam(required = false) String carName) {
        return CommonResult.success(cmsCarService.getCarListByStoreId(storeId,carName));
    }

    @GetMapping("/{id}")
    @ApiOperation("获取网点详情")
    public CommonResult<PmsStore> storeObj(@ApiParam(name = "id", value = "网点标识", required = true)
                                        @PathVariable Long id) {
        return CommonResult.success(storeService.detail(id));
    }

    @GetMapping("/page")
    @ApiOperation("获取网点分页")
    public CommonResult<CommonPage<PmsStoreVo>> storePage(CommonQueryParam commonQueryParam) {
        return CommonResult.success(CommonPage.restPage(storeService.pageList(commonQueryParam)));
    }
}
